package com.miplus.ccrm.core.service.commission;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.core.controller.vo.commission.AgentCountEditVo;
import com.miplus.ccrm.core.controller.vo.commission.AgentCountReqVo;
import com.miplus.ccrm.core.controller.vo.commission.AgentCountResVo;
import com.miplus.ccrm.core.controller.vo.commission.CommissionRatioResVo;
import com.miplus.ccrm.core.pojo.FiCommissionAgentCount;
import com.miplus.ccrm.core.pojo.FiCommissionAgentMode;
import com.miplus.ccrm.core.service.commission.FiCommissionAgentCountService;
import com.miplus.ccrm.core.mapper.FiCommissionAgentCountMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
* @author wangzl
* @description 针对表【fi_commission_agent_count】的数据库操作Service实现
* @createDate 2025-03-01 11:38:35
*/
@Service
public class FiCommissionAgentCountServiceImpl extends MPJBaseServiceImpl<FiCommissionAgentCountMapper, FiCommissionAgentCount>
    implements FiCommissionAgentCountService{
    @Resource
    FiCommissionAgentModeService fiCommissionAgentModeService;
    @Override
    public IPage<AgentCountResVo> getAgentCountList(AgentCountReqVo para)
    {
        return selectJoinListPage(
                new Page<>(para.getPageNum(),para.getPageSize()),
                AgentCountResVo.class,
                new MPJLambdaWrapper<FiCommissionAgentCount>()
                        .leftJoin("sys_grp g on g.grpid = t.dept")
                        .eq(StringUtils.isNotEmpty(para.getCommissionMonth()),FiCommissionAgentCount::getCommissionMonth,para.getCommissionMonth())
                        .eq(StringUtils.isNotEmpty(para.getDept()),FiCommissionAgentCount::getDept,para.getDept())
                        .eq(StringUtils.isNotEmpty(para.getDisttag()),FiCommissionAgentCount::getDisttag,para.getDisttag())
                        .selectAll(FiCommissionAgentCount.class)
                        .selectAs("fun_getnames('SORTING.QUEUE',t.disttag)",AgentCountResVo::getDisttagDesc)
                        .selectAs("g.grpname", AgentCountResVo::getDeptName)
                        .orderByDesc(FiCommissionAgentCount::getCommissionMonth)
        );
    }
    @Override
    public CmdResVo addAgentCount(String usrId, FiCommissionAgentCount fiCommissionAgentCount)
    {
        if(StringUtils.isEmpty(fiCommissionAgentCount.getDisttag()))
            return new CmdResVo(-1, "未设置参数委案队列");
        FiCommissionAgentMode fiCommissionAgentMode =
                fiCommissionAgentModeService.getAgentModeByDisttag(fiCommissionAgentCount.getDisttag(),fiCommissionAgentCount.getCommissionMonth());
        //坐席计算方式 1:统计，2：配置
        if(fiCommissionAgentMode == null || fiCommissionAgentMode.getAgentmode() != 2)
        {
            return new CmdResVo(-1, fiCommissionAgentCount.getDisttag() + "队列不是系统配置模式");
        }
        fiCommissionAgentCount.setId(null);
        fiCommissionAgentCount.setCrdt(new Date());
        fiCommissionAgentCount.setCrusr(usrId);
        save(fiCommissionAgentCount);
        return new CmdResVo(1,fiCommissionAgentCount.getId().toString());
    }
    @Override
    public CmdResVo updateAgentCount(String usrId, AgentCountEditVo para)
    {
        if( ObjectUtils.isNull(para.getId()))
        {
            return new CmdResVo(-1, "没有指定更新的ID");
        }
        FiCommissionAgentCount fiCommissionAgentCount = new FiCommissionAgentCount();
        BeanUtils.copyProperties(para,fiCommissionAgentCount);
        fiCommissionAgentCount.setMddt(new Date());
        fiCommissionAgentCount.setMdusr(usrId);
        updateById(fiCommissionAgentCount);
        return new CmdResVo(1, "更新成功");
    }
    @Override
    public CmdResVo deleteAgentCount(Integer id)
    {
        removeById(id);
        return new CmdResVo(1, "更新成功");
    }
}




